
using AutoMapper;
using DTO;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Logging;
using Model;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection;
using System.Security.Claims;
using System.Web;
using Util;

namespace API.Controllers
{
    /// <summary>
    /// 登录
    /// </summary>
    public class LoginController : BaseController
    {
        Repository<tBase_Role> _Base_Role;
        readonly IMapper _mapper;
        public LoginController(Repository<tBase_Role> Base_Role, IMapper mapper)
        {
            _Base_Role = Base_Role;
            _mapper = mapper;
        }
        /// <summary>
        /// 登录获取token
        /// </summary>
        /// <param name="Account"></param>
        /// <param name="Password">4QrcOUm6Wau+VuBX8g+IPg==/ICy5YqxZB1uWSwcVLSNLcA==</param>
        /// <returns></returns>role
        [HttpGet]
        [AllowAnonymous]
        public async Task<ResultMsg<string>> Login(string Account, string Password)
        {
            ResultMsg<string> result= new ResultMsg<string>();
            try
            {
                string xx = HttpUtility.UrlEncode(Password);
                var res = HttpHelper.HttpGet($"https://app.yingdl.com:9527/QA_Login/Login?UserNumber={Account}&Password={xx}");
                JObject jo = JObject.Parse(res);
                if (jo == null)
                {
                    return result.Error("登录异常");
                }
                if (Convert.ToInt32(jo["code"]) == 200)
                {
                    int UserId = Convert.ToInt32(jo["data"]["UserID"]);
                    string FSystemRole = jo["data"]["FSystemRole"].ToString();

                    var claims = new List<Claim>();

                    claims.Add(new Claim("Uid", UserId.ToString()));
                    claims.Add(new Claim("Role", FSystemRole));
                    JwtHelper helper = new JwtHelper();
                    string token = helper.BuildToken(claims);

                    return result.Success(token);
                }
                else
                {
                    return result.Error("登录异常");
                }
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("Login:" + ex.Message);
                return result.Exception();
            }
        }

        /// <summary>
        /// 解析token
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ResultMsg<TokenModel>> SerializeToken()
        {
            ResultMsg<TokenModel> result = new ResultMsg<TokenModel>();
            try
            {
                //需要截取Bearer 
                var tokenHeader = HttpContext.Request.Headers["Authorization"].ToString().Replace("Bearer ", "");
                TokenModel dto=JwtHelper.SerializeJwt(tokenHeader);
                return result.Success(dto);
            }
            catch (Exception ex)
            {
                Util.NLOG.LogHelper.Error("SerializeToken:" + ex.Message);
                return result.Exception();
            }
        }
    }
}
